<template>
	<view>
		<!-- style="height: calc(100vh - 120px);" -->
		<view >
			<view style="login-top">
				<image style="width: 100vw;" :src="loginPic"></image>
			</view>
			<view class="padding login-bd form-cust">
				<view class="cu-form-group">
					<view class="title">手机号</view>
					<input @focus="show=false" @blur="show=true" placeholder="输入手机号" name="input" v-model="user.phoneNo"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">验证码</view>
					<input @focus="show=false" @blur="show=true" placeholder="输入短信验证码" name="input" v-model="user.checkCode"></input>
					<button v-if="send" class='cu-btn  shadow login-code' @tap="getCode">获取验证码</button>
					<button v-else class='cu-btn'> {{timerCount}}秒后重新获取</button>
				</view>
				<!-- <view class="cu-form-group">
						<view class="title">密码</view>
						<input placeholder="输入密码" name="input" type="password" v-model="user.passwd"></input>
					</view> -->
				<view class="text-center padding-top-sm margin-top marign-bottom">
					<radio @click="agree=!agree" :checked="agree"></radio>
					<text class="margin-left-sm">已阅读并同意</text>
					<text class="text-blue" @tap="pol">《服务协议》</text>
					<text class="text-blue" @tap="pri">《隐私政策》</text>
				</view>
				<view class="margin-top">
					<!-- :disabled="!agree" -->
					<button :class="!agree?'btn-disabled':'btn-active'"  @tap="tologin">登录</button>
				</view>
			</view>
		</view>
			<view  class="text-center margin-top-xl ">
				<text @tap="contact" class="block text-blue padding-bottom-sm">登录遇到问题 ? 联系我们</text>
				<text v-if="show" class="block text-gray">当前版本：{{ version }}</text>
			</view>
	</view>
</template>

<style>

</style>
<script>
	import {isWx, location} from '../../utils/util.js';
	export default {
		data() {
			return {
				agree: false,
				show: true,
				user: {
					//accNum:'18779213482',
					phoneNo: '',
					checkCode: '',
					//passwd:'51zubus'
					invitedCode: '', //被别人邀请注册的 被邀请码
					code: ''
				},
				send: true,
				userInfo: {},
				timerCount: 60,
				inter: '',
				version: '1.0.25',
				defaultPic:"../../static/login.jpg",
				loginPic:'',
			}
		},
		onLoad(option) {
			let uinfo = uni.getStorageSync('ONLINE_USER');
			if(uinfo){
				this.user.phoneNo = uinfo.phoneNo;
			}
			if(isWx()){
				if(option.code){
					this.user.code = option.code;
				}else{
					// 判断是否绑定微信
					let host = window.location.protocol+"//"+window.location.host;
					window.location.replace(host + '/code.html?page=login');
				}
			}
			this.getLoginPic()
			let _this = this;
			this.baseRequest('api/driver/app/checkVersion', 'GET', {}, function(res, info) {
				_this.version = res.version;
			})
			
		},
		methods: {
			contact(){
				uni.navigateTo({
					url: '/pages/user/contact'
				});
			},
			pol(){
				uni.navigateTo({
					url: '/pages/login/pol'
				})
			},
			pri(){
				uni.navigateTo({
					url: '/pages/login/pri'
				})
			},
			getCode() {
				if (this.user.phoneNo == '' || (!(/^1[3456789]\d{9}$/.test(this.user.phoneNo)))) {
					uni.showToast({
						icon: 'none',
						title: '手机号错误'
					});
					return;
				}

				let _this = this;
				this.baseRequest('/sendMsg/' + this.user.phoneNo, 'POST', this.user, function(res, info) {
					uni.showToast({
						icon: 'none',
						title: '验证码已发送'
					})
					_this.send = false;
					_this.inter = setInterval(() => {
						_this.count();
					}, 1000);
				});
			},
			count() {
				if (this.timerCount < 1) {
					clearInterval(this.inter);
					this.timerCount = 60;
					this.send = true;
					return;
				}
				this.timerCount--;
			},
			tologin() {
				let _this = this;
				if(!_this.agree){
					uni.showToast({
						icon: 'error',
						title: '请先勾选服务协议'
					})
					return
				}
				let data = location();
				this.user.areaName = data.attenCity;
				this.user.address = data.address;
				this.baseRequest('/app/phoneSign', 'POST', this.user, function(res, info) {
					_this.userInfo = res.data;
					//this.userInfo= JSON.stringify(res.data.data);
					uni.setStorageSync('ONLINE_USER', _this.userInfo);
					uni.setStorageSync('Authorization', res.Authorization);
					
					// 上报用户信息
					if(typeof androlua == "undefined"){
						
					}else{
						let user = res.data;
						let Authorization = res.Authorization;
						let data = {
							userID: user.userID,
							userName: user.userName,
							wxnickName: user.wxnickName,
							phoneNo: user.phoneNo,
							Authorization: Authorization
						}
						androlua.pageSetUserInfo(JSON.stringify(data));
						// androlua.pageGoBack();
					}
					
					uni.reLaunch({
						url: '/pages/index/index'
					});
				});
			},
			log(){
				// 记录活跃和定位信息
				let data = location();
				let _this = this
				this.baseRequest('api/dau/log', 'GET', {}, function(res, info) {
					_this.baseRequest('api/dau/log/user', 'POST', {areaName: data.attenCity}, function(res, info) {
					}, function(){});
				}, function(){});
			},
			getLoginPic(){
				let _this = this
				this.baseRequest('api/parm/', 'GET', {parmName:'loginPic'}, function(res, info) {
					_this.loginPic = res&&res.length>0?res[0].value:_this.defaultPic
				})
			},
		}
	}
</script>

<style>
	.login-top {
		height: 120px;
	}
	.btn-disabled{
		background-color:rgba(0,122,255,.6);
		color:hsla(0,0%,100%,.6)
	}
	.btn-active{
		background-color: #007aff;
		color:#fff;
	}
	.login-code {
		font-size: 16px;
		padding: 0 12px;
		background: #F2F2F2;
		color: #0081FF
	}
</style>
